/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daoBeans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import modelo.Empresa;

/**
 *
 * @author Camilo
 */
public class daoEmpresa {
    
    Connection link;
    Statement st;
    PreparedStatement pst;
    ResultSet rs;

    // Conexión MYSQL
    
    public daoEmpresa(Connection link) {
        this.link = link;
        try {
            st = link.createStatement();
           
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    //Creación
    
    public int crearEmpresa(Empresa e){
        int n = 0;
        try {
            pst = link.prepareStatement("INSERT INTO Empresa(Nombre, NIT, Tipo, Padre) " +
               "VALUES(?, ?, ?, ? )");
            pst.setString(1, e.getNombre());
            pst.setString(2, e.getNit());
            pst.setString(3, e.getTipo());
            pst.setString(4, e.getPadre());
            n = pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return n;
    }
    
    // Busqueda
    
    public Empresa buscarEmpresaNombre(String nombre){
        Empresa e = new Empresa();
        
        try {
            rs = st.executeQuery("SELECT Nombre, NIT, Tipo FROM empresa " +
                    "WHERE Nombre LIKE '%"+nombre+"%'");
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rs.first();
            e.setNombre(rs.getObject(1).toString());
            e.setNit(rs.getObject(2).toString());
            e.setTipo(rs.getObject(3).toString());
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        return e;
    }
    
    public Empresa buscarEmpresaNIT(String Nit){
        Empresa e = new Empresa();
        
        try {
            rs = st.executeQuery("SELECT Nombre, NIT, Tipo FROM empresa " +
                    "WHERE NIT LIKE '%"+Nit+"%'");
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rs.first();
            e.setNombre(rs.getObject(1).toString());
            e.setNit(rs.getObject(2).toString());
            e.setTipo(rs.getObject(3).toString());
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        return e;
    }
    
    public ArrayList<Empresa> buscarEmpresaTipo(String Tipo){
        ArrayList<Empresa> es = new ArrayList<Empresa>();
                
        try {
            rs = st.executeQuery("SELECT Nombre, NIT, Tipo FROM empresa " +
                    "WHERE Tipo LIKE '%"+Tipo+"%'");
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while(rs.next()){
                Empresa e = new Empresa();
                e.setNombre(rs.getObject(1).toString());
                e.setNit(rs.getObject(2).toString());
                e.setTipo(rs.getObject(3).toString());
                es.add(e);
            }
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        return es;
    }
    
    // Actualización
    
    public int actualizarEmpresa(Empresa e){
        int n = 0;
        try {
            pst = link.prepareStatement("UPDATE empresa "
                    + "SET "
                    + "Nombre = ? "
                    + "Nit = ? "
                    + "Tipo = ? "
                    + "Where NIT = " + e.getNit());
            pst.setString(1, e.getNombre());
            pst.setString(2, e.getNit());
            pst.setString(3, e.getTipo());
            n = pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(daoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return n;
    }
    
    // Eliminación
    
    public int eliminarEmpresa(String Nit){
        
        int n = 0;
        
        try {
            st.executeUpdate("DELETE from Empresa WHERE NIT = '" + Nit + "'");
            n = 1;
        } catch (SQLException ex) {
            Logger.getLogger(daoEmpresa.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return n;
        
    }
    
}
